<?php
/**
 * Created by PhpStorm.
 * User: fang_
 * Date: 2016/9/29
 * Time: 14:16
 */
require 'vendor/autoload.php';

use Sunra\PhpSimple\HtmlDomParser;

$client = new GuzzleHttp\Client();


// 获取session_id
$response = $client->get('http://e-learning.hznu.edu.cn/eol/homepage/common/index.jsp');
$session_id = $response->getHeader('Set-Cookie', true)[0];
$session_id = strstr($session_id, ' ', true);

setcookie("session_id",substr($session_id,0,-1), time()+3600);

// 模拟登陆
$res = $client->request('POST', 'http://e-learning.hznu.edu.cn/eol/homepage/common/login.jsp',
    [
        'headers' => ['Cookie' => substr($session_id,0,-1)],
        'form_params' => [
            'IPT_LOGINUSERNAME' => ', // TODO 用户名
            'IPT_LOGINPASSWORD' => ' // TODO 密码
        ]
    ]
);

$res = $client->request('POST', 'http://e-learning.hznu.edu.cn/eol/tea_left_course_menu.jsp?lid=22507&colum=%BF%CE%B3%CC%D7%F7%D2%B5&_=1475134130946',
    [
        'headers' => ['Cookie' => substr($session_id,0,-1)]
    ]
);

$res = $client->request('POST', 'http://e-learning.hznu.edu.cn/eol/common/hw/hwtask.jsp',
    [
        'headers' => ['Cookie' => substr($session_id,0,-1)]
    ]
);

$html =  $res->getBody();

$html =  iconv('GB2312', 'UTF-8', $html);;
// 重新设计界面

// 获取作业列表
$html = HtmlDomParser::str_get_html($html);

$table = $html->find('form[name=form1] table tbody tr[align=center]');

$assignment_array = array();
foreach ($table as $element)
{
    $assignment_array[] = array(
        'name'=>$element->find('td[align=left] a',0)->plaintext,
        'publish_time'=>$element->find('td',2)->plaintext,
        'status'=>trim($element->find('td',3)->plaintext),
        'commit_number'=>$element->find('td',6)->plaintext,
        'mark_number'=>$element->find('td',7)->plaintext,
        'mark_link'=>strstr($element->find('td',10)->first_child()->href,'?')
    );
}
//print_r($assignment_array);

// 转入每次作业批阅页面
$res = $client->request('POST',
    'http://e-learning.hznu.edu.cn/eol/common/hw/hwanswer.jsp?hwtid=7684&restart=true',
    [
        'headers' => ['Cookie' => substr($session_id,0,-1)]
    ]
);

$html = HtmlDomParser::str_get_html(iconv('GBK', 'UTF-8', $res->getBody()));
//echo $html;
// 提取未批阅信息
$unmark_array = array();

$table = $html->find('form[name=form1] table.infotable_hr tbody tr[align=center]');

foreach ($table as $element)
{
    $unmark_array[] = array(
        'name'=>$element->find('td',2)->plaintext,
        'student_id'=>$element->find('td',1)->plaintext,
        'commit_time'=>$element->find('td',3)->plaintext,
        'score'=>trim($element->find('td',4)->plaintext),
        'mark_link'=>$element->find('td',8)->first_child()->href
    );
}
//print_r($unmark_array);

?>

<html>
    <head lang="zh-cn">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Java课程批阅小助手</title>

        <!-- 新 Bootstrap 核心 CSS 文件 -->
        <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
    <div class="container text-center">
        <h1>Java课程批阅小助手</h1>



        <!-- 显示所有的实验-->
        <table class="table">
            <tr>
                <th>实验名称</th>
                <th>截止时间</th>
                <th>状态</th>
                <th>提交</th>
                <th>未批阅</th>
                <th>批阅链接</th>
            </tr>
            <?php
            foreach ($assignment_array as $assgin)
            {
                echo '<tr>';
                if ($assgin['status'] == "发布")
                {
                    echo '<td>'.$assgin['name'].'</td>';
                    echo '<td>'.$assgin['publish_time'].'</td>';
                    echo '<td>'.$assgin['status'].'</td>';
                    echo '<td>'.$assgin['commit_number'].'</td>';
                    echo '<td>'.$assgin['mark_number'].'</td>';
                    echo '<td><a href=permark.php'.$assgin['mark_link'].'&name='.$assgin['name'].'>批阅</a>'.'</td>';
                }
                echo '</tr>';
            }
    ?>
        </table>
    </div>



    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    </body>
</html>
